import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:flutter_i18n/flutter_i18n.dart';
import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';
import 'package:jm_flutter/src/config.dart';

import 'logcat.dart';

late BuildContext _I_18N_Context;

const kapplanguege = "kapplanguege";

class I18n {
  static const tag = "I18n";
  static final gs = GetStorage();
  static String _systemLanguageCode = "";
  static String _languageCode = GetStorage().read(kapplanguege) ?? "system";
  static late Locale _locale;
  static late FlutterI18nDelegate flutterI18nDelegate;
  static final List<AppLanguage> _languages = [
    AppLanguage(name: "跟随系统", code: "system"),
    AppLanguage(name: "English", code: "en"),
    AppLanguage(name: "简体中文", code: "zh"),
    //AppLanguage(name: "繁体中文", code: "zh_TW"),
    //AppLanguage(name: "日本語", code: "ja"),
    //AppLanguage(name: "한국어", code: "ko"),
    //AppLanguage(name: "العربية", code: "ar"),
  ];

  static List<AppLanguage> get languages => _languages;

  static String get languageCode => _languageCode;

  static String get systemLanguage => _systemLanguageCode;

  static Locale get locale => _locale;

  static bool get isSystemCn {
    if (_systemLanguageCode.contains("zh")) {
      return true;
    }

    return false;
  }

  static void setLanguages(List<AppLanguage> list,{bool system = true}) {
    _languages.clear();
    if(system){
      _languages.add(AppLanguage(name: "跟随系统", code: "system"));
    }
    _languages.addAll(list);
  }

  static TransitionBuilder initBuilder(TransitionBuilder builder) {
    return (BuildContext context, Widget? child) {
      _I_18N_Context = context;
      return builder(context, child);
    };
  }

  static void init() {
    _languageCode = gs.read(kapplanguege) ?? "system";
    logd(tag, "当前APP语言:" + _languageCode);
    flutterI18nDelegate = FlutterI18nDelegate(
      translationLoader: FileTranslationLoader(
        useCountryCode: false,
        fallbackFile: JmConfig.i18nFallbackFile,
        basePath: JmConfig.i18nBasePath,
        forcedLocale: Locale(_getLanguageCodeStr()),
      ),
      missingTranslationHandler: _missingTranslationHandler,
    );
  }

  static void setLanguage(AppLanguage appLanguage) async {
    logd(tag, "修改语言:" + _languageCode + "->" + appLanguage.code);
    if (_languageCode != appLanguage.code) {
      _languageCode = appLanguage.code;
      gs.write(kapplanguege, _languageCode);
      await FlutterI18n.refresh(_I_18N_Context, Locale(_getLanguageCodeStr()));
      Get.updateLocale(Locale(_getLanguageCodeStr()));
    }
  }

  static String _getLanguageCodeStr() {
    String langStr = "en";
    _systemLanguageCode = window.locale.languageCode;
    if (_languageCode == "system") {
      _locale = window.locale;

      var countryCode = window.locale.countryCode ?? "";
      var scriptCode = window.locale.scriptCode ?? "";
      logd(
          tag,
          "当前系统语言:" +
              _systemLanguageCode +
              ", " +
              countryCode +
              ", " +
              scriptCode +
              ", " +
              _systemLanguageCode.indexOf("zh").toString());

      if (_systemLanguageCode.isNotEmpty) {
        langStr = _systemLanguageCode;
      } else {
        langStr = "en";
      }

      if (langStr == "zh" &&
          (countryCode == "TW" ||
              scriptCode == "Hant" ||
              countryCode == "HK" ||
              countryCode == "MO")) {
        langStr = "zh_TW";
      }
    } else {
      langStr = _languageCode;
      _locale = Locale(langStr);
    }

    return langStr;
  }

  static void _missingTranslationHandler(String key, Locale? locale) {
    logw(tag, "缺少翻译 Key: $key, languageCode: ${locale!.languageCode}");
  }
}

class AppLanguage {
  String name;
  String code;

  AppLanguage({this.name = "", this.code = ""});
}

String i18n(final String key) {
  if (JmConfig.i18nEnable) {
    return FlutterI18n.translate(_I_18N_Context, key);
  }
  return key;
}

extension String_i18n on String {
  String get i18n {
    if (JmConfig.i18nEnable) {
      return FlutterI18n.translate(_I_18N_Context, this);
    }
    return this;
  }
}
